#include "gl_window.h"

#include <glad/glad.h>
#include <GLFW/glfw3.h>

GlWindow::GlWindow(const char *title, int w, int h)
{
    window_ = glfwCreateWindow(w, h, title, nullptr, nullptr);

    glfwMakeContextCurrent(window_);

    glfwSetWindowSizeCallback(window_, myGlfwSizeCb);

    gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress));
}

GlWindow::~GlWindow()
{
    glfwDestroyWindow(window_);
    window_ = nullptr;
}

bool GlWindow::shouldClose() const
{
    return glfwWindowShouldClose(window_);
}

void GlWindow::frameUpdate()
{
    myGlfwInputProcess();

    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    render();

    glfwSwapBuffers(window_);
    glfwPollEvents();
}

void GlWindow::render()
{
}

void GlWindow::myGlfwSizeCb(GLFWwindow *window, int w, int h)
{
    glViewport(0, 0, w, h);
}

void GlWindow::myGlfwInputProcess()
{
    if (glfwGetKey(window_, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
        glfwSetWindowShouldClose(window_, true);
    }
}

